package com.iclub.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.iclub.model.Message;
import com.iclub.service.MessageService;

/**

 *
 */
@Controller
@RequestMapping("/msg")
public class MessageController extends BaseController {

	// 消息入库
	@RequestMapping("/insertMessage.do")
	public void insertMessage(HttpServletRequest request, HttpServletResponse response, Message msg)
			throws IOException {
		log_detail.info("[消息入库] 客户端请求数据为：" + msg);
		Map<String, Object> res = messageServiceImpl.insertMessage(msg);
		log_detail.info("[消息入库] 服务端响应数据为：" + res);
		String json = JSON.toJSONString(res);
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(json);
	}

	// 更新消息状态
	@RequestMapping("/updateMessageState.do")
	public void updateMessageState(HttpServletRequest request, HttpServletResponse response, String msgId)
			throws IOException {
		log_detail.info("[更新消息状态] 客户端请求数据为：" + msgId);
		Map<String, Object> res = messageServiceImpl.updateMessageState(Integer.parseInt(msgId));
		log_detail.info("[更新消息状态] 服务端响应数据为：" + res);
		String json = JSON.toJSONString(res);
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(json);
	}

	// 获取消息列表
	@RequestMapping("/getMessageList.do")
	public void selectMessageList(HttpServletRequest request, HttpServletResponse response,
			String userId) throws IOException {
		// 把对象接收的参数转化为map
		Map<String, String> req = new HashMap<String, String>();
		req.put("senderId", userId);
		req.put("receiverId", userId);
		log_detail.info("[获取消息列表] 客户端请求数据为：" + req);
		Map<String, Object> res = messageServiceImpl.selectMessageList(req);
		String json = JSON.toJSONString(res);
		log_detail.info("[获取消息列表] 服务端响应数据为：" + json);
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(json);
	}

	
}
